import { functionDeclaration } from "babel-types";
/**
 * Created by liufeng on 2020/2/6.
 */
export class MigrationMap {
    private options: any;
    private myChart: any;

    constructor(options: any) {
        this.options = options;
    }

    public async addMigrationMap(areaId: any, date: any, migrateType: any) {
        if (areaId === "000000") {
            const list: any = await this.options.service.findMigratePeople(areaId, date, migrateType);
            if (list.data.length !== 0) {
                this.addScatterRoute(list.data);
            }
        } else {
            const list: any = await this.options.service.getMigrateMap(areaId, date, migrateType);
            if (list.data.length !== 0) {
                this.addFlyRoute(list.data);
            }
        }
    }

    public removeMigrationMap() {
        if (!!this.myChart) {
            this.myChart.unbind();
            this.myChart = null;
        }
    }

    public unload() {
        this.removeMigrationMap();
    }

    private addFlyRoute(list: any) {
        this.removeMigrationMap();
        const data = createGeoJson(list);
        const geoSource = new gv.source.GeoJsonSource(data);
        const myChart = new gv.chart.QianXiRippleChart(geoSource, {
            shadowBlur: 0,
            strokeStyle: 'rgba(255,2,2,0.2)',
            pointStyle: {
                shadowBlur: 0,
                symbolSize: (val: any, b: any) => {
                    if (b.data.count <= 5) {
                        return 5;
                    } else {
                        return b.data.count;
                    }
                },
                strokeStyle: 'rgba(0,0,255,0.2)',
                rippleEffect: {
                    brushType: 'stroke',
                    scale: 3
                },
                label: {
                    show: true,
                    fontSize: 10
                },
            },
            flowAnimation: {
                period: 3,
                trailLength: 0.7,
                symbolSize: 8,
                symbol: 'circle'
            }
        });
        this.myChart = myChart;
        myChart.bind(this.options.map, gv.chart.MapType.TSMap);
        myChart.render();
        myChart.startRichText();
        this._flyFit(data);// 迁徙图视野

        function dealWithCoordinate(coor: any) {
            if (!!coor) {
                const dataArr = coor.split(",");
                dataArr[0] = parseFloat(dataArr[0]);
                dataArr[1] = parseFloat(dataArr[1]);
                return dataArr;
            }
        }

        function createGeoJson(dataList: any) {
            const geoJson: any = {
                type: 'FeatureCollection',
                crs: {
                    type: 'name',
                    properties: {
                        name: 'urn:ogc:def:crs:OGC:1.3:CRS84',
                    },
                },
                features: [],
            };
            for (let i = 0; i < dataList.length; i++) {
                const item = dataList[i];
                geoJson.features.push({
                    type: 'Feature',
                    properties: {
                        from: item.fromName,
                        to: item.toName,
                        count: Math.ceil(item.value)
                    },
                    geometry: {
                        type: "LineString",
                        coordinates: [dealWithCoordinate(item.from), dealWithCoordinate(item.to)]
                    }
                })
            }
            return geoJson;
        }
    }

    private addScatterRoute(list: any) {
        this.removeMigrationMap();
        const data = createGeoJson(list);
        const situationSource = new gv.source.GeoJsonSource(data);

        const myChart = new gv.chart.SituationChart(situationSource, {
            tooltip: {
                formatter: (val: any) => {
                    return val.marker + " " + val.name + ":" + val.data[3]
                }
            },
            xAxis: {
                show: false
            },
            bar: {
                itemStyle: {
                    opacity: 0
                }
            },
            yAxis: {
                show: false,
                axisLabel: {
                    textStyle: {
                        color: "#aaa"
                    }
                },
                axisLine: {
                    show: false
                }
            }
        });
        myChart.config("series[2].show", false);
        myChart.bind(this.options.map, gv.chart.MapType.TSMap);
        myChart.render();
        this.myChart = myChart;
        this._flyFit(data);

        function toPoint(percent: any) {
            let str = percent.replace("%", "");
            str = str / 100;
            return str;
        }

        function createGeoJson(dataList: any) {
            const geoJson: any = {
                type: "FeatureCollection",
                crs: {
                    type: "name",
                    properties: {
                        name: "urn:ogc:def:crs:OGC:1.3:CRS84"
                    }
                },
                features: []
            }
            for (let i = 0; i < dataList.length; i++) {
                const item = dataList[i];
                geoJson.features.push({
                    type: 'Feature',
                    properties: {
                        name: item.area,
                        weight: Math.round(toPoint(item.percent) * 1000) * 2
                    },
                    geometry: {
                        type: "Point",
                        coordinates: [parseFloat(item.latitude), parseFloat(item.longitude)]
                    }

                })
            }
            return geoJson;
        }
    }

    // 飞行路线调整视野
    private _flyFit(geojson: any) {
        const gFeatureCollection = g2.sfs.GFeatureFormatter.fromGeoJson(geojson, 4326);
        const envelope = new g2.sfs.Envelope({ spatialReference: 4326 });
        for (let i = 0, len = gFeatureCollection.size(); i < len; i++) {
            const feature = gFeatureCollection.get(i);
            const geometry = feature.getGeometry();
            envelope.union(geometry.envelope());
        }
        this.options.map.pan(envelope, new Array(4).fill(100));
    }
}
